/*****************************************************************************************
 *                                                                                       *
 * OpenSpace                                                                             *
 *                                                                                       *
 * Copyright (c) 2014-2020                                                               *
 *                                                                                       *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this  *
 * software and associated documentation files (the "Software"), to deal in the Software *
 * without restriction, including without limitation the rights to use, copy, modify,    *
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to    *
 * permit persons to whom the Software is furnished to do so, subject to the following   *
 * conditions:                                                                           *
 *                                                                                       *
 * The above copyright notice and this permission notice shall be included in all copies *
 * or substantial portions of the Software.                                              *
 *                                                                                       *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,   *
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A         *
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT    *
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  *
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE  *
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                         *
 ****************************************************************************************/

#version 460  core

; // preprocessor add semicolon to isolate error messages
#line 1 1 // C:\os\clean\OpenSpace-0.16.0\shaders\PowerScaling\powerScalingMath.hglsl
/*****************************************************************************************
 *                                                                                       *
 * OpenSpace                                                                             *
 *                                                                                       *
 * Copyright (c) 2014-2020                                                               *
 *                                                                                       *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this  *
 * software and associated documentation files (the "Software"), to deal in the Software *
 * without restriction, including without limitation the rights to use, copy, modify,    *
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to    *
 * permit persons to whom the Software is furnished to do so, subject to the following   *
 * conditions:                                                                           *
 *                                                                                       *
 * The above copyright notice and this permission notice shall be included in all copies *
 * or substantial portions of the Software.                                              *
 *                                                                                       *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,   *
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A         *
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT    *
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  *
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE  *
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                         *
 ****************************************************************************************/

#ifndef POWERSCALING_MATH_HGLSL
#define POWERSCALING_MATH_HGLSL


const float k = 10.0;
const float FLT_MAX = 1e38; // Not max but large enough for the purpose

float log10(float x) {
    return log(x) / log(10.0);
}

vec4 psc_normalization(vec4 v) {
    vec4 v2 = v;
    float l = length(v.xyz);
    v2.xyz = v.xyz / l;
    v2.w = v.w - log10(1.0/l);
    return v2;
}

vec4 psc_addition(vec4 v1, vec4 v2) {
    float ds = v2.w - v1.w;
    if(ds >= 0) {
        float p = pow(k,-ds);
        return vec4(v1.x*p + v2.x, v1.y*p + v2.y, v1.z*p + v2.z, v2.w);
    } else {
        float p = pow(k,ds);
        return vec4(v1.x + v2.x*p, v1.y + v2.y*p, v1.z + v2.z*p, v1.w);
    }
}

vec4 psc_subtraction(vec4 v1, vec4 v2) {
    vec4 negV2 = vec4(-v2.xyz, v2.w);
    return psc_addition(v1, negV2);
}

vec2 psc_addition(vec2 v1, vec2 v2) {
    float ds = v2.y - v1.y;
    if(ds >= 0) {
        float p = pow(k,-ds);
        return vec2(v1.x*p + v2.x, v2.y);
    } else {
        float p = pow(k,ds);
        return vec2(v1.x + v2.x*p, v1.y);
    }
}

vec2 psc_subtraction(vec2 v1, vec2 v2) {
    vec2 negV2 = vec2(-v2.x, v2.y);
    return psc_addition(v1, negV2);
}


vec4 z_normalization(vec4 v_in) {
    vec4 v_out = v_in;
    v_out.z = 0;
    //if ( v_out.z < -v_out.w )
    //  v_out.z = -v_out.w;
    //else if (v_out.z > v_out.w)
    //  v_out.z = v_out.w;
    return v_out;
}

/**
 * Convert a psc vector to a float
 */
vec3 pscToLinear(vec4 position) {
    return pow(k, position.w) * position.xyz;
}

/**
 * Convert a psc scalar to a float
 */
float pscToLinear(vec2 position) {
    return pow(k, position.y) * position.x;
}

#endif
; // preprocessor add semicolon to isolate error messages
#line 28 0 // C:\os\clean\OpenSpace-0.16.0\modules\space\shaders\debrisViz_vs.glsl

layout (location = 0) in vec4 vertex_data; // 1: x, 2: y, 3: z, 4: timeOffset,
layout (location = 1) in vec2 orbit_data; // 1: epoch, 2: period

uniform dmat4 modelViewTransform;
uniform mat4 projectionTransform;

//uniform float lineFade;
uniform double inGameTime;

out vec4 viewSpacePosition;
out float vs_position_w;

out float periodFraction_f;
out float offsetPeriods;
out float vertexID_f;

// debugers :
// out float offset;
// out float epoch;
// out float period;
// out double tajm;

void main() {

    // tajm = inGameTime;
    /** The way the position and line fade is calculated is:
    *   By using inGameTime, epoch and period of this orbit,
    *   we get how many revolutions it has done since epoch.
    *   The fract of that, is how far into a revolution it has traveld since epoch.
    *   Similarly we do the same but for this vertex, but calculating offsetPeriods.
    *   In the fragment shader the difference between
    *   periodFraction_f and offsetPeriods is calculated to know how much to fade
    *   that specific fragment.
    */

    // If orbit_data is doubles, cast to float first
    float offset = vertex_data.w;
    double epoch = orbit_data.x;
    double period = orbit_data.y;
  
    // calculate nr of periods, get fractional part to know where
    // the vertex closest to the debris part is right now
    double nrOfRevolutions = (inGameTime - epoch) / period;
    //double periodFraction = fract(nrOfRevolutions); //mod(nrOfRevolutions, 1.0);
    int intfrac = int(nrOfRevolutions);
    double doublefrac = double(intfrac);
    double periodFraction = nrOfRevolutions - doublefrac;
    if (periodFraction < 0.0) {
        periodFraction += 1.0;
    }
    periodFraction_f = float(periodFraction);

    // same procedure for the current vertex
    float period_f = float(period);
    offsetPeriods = offset / period_f;

    // offsetPeriods can also be calculated by passing the vertexID as a float
    // to the fragment shader and deviding it by nrOfSegments.
    vertexID_f = float(gl_VertexID);
    dvec3 positions = dvec3(vertex_data.x, vertex_data.y, vertex_data.z);
    dvec4 vertexPosition = dvec4(positions, 1);
    viewSpacePosition = vec4(modelViewTransform * vertexPosition);
    vec4 vs_position = z_normalization( projectionTransform * viewSpacePosition);
    gl_Position = vs_position;
    vs_position_w = vs_position.w;

}





